﻿CREATE PROCEDURE [dbo].[categories_validate]
    @categoryId int = 0,
    @name nvarchar(256) = null
AS
BEGIN
    If (@categoryId = 0)
        RETURN CASE
            WHEN EXISTS(
                SELECT Categories.CategoryId
                FROM Categories
                WHERE Categories.Name = @name)
            THEN 0 
            ELSE 1
        END
    ELSE
        RETURN CASE
            WHEN EXISTS(
                SELECT Categories.CategoryId
                FROM Categories
                WHERE Categories.Name = @name AND Categories.CategoryId != @categoryId)
            THEN 0 
            ELSE 1
        END
END